import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_app/config/colorconfig.dart';
import 'package:flutter_app/config/widgetconfig.dart';
import 'package:like_button/like_button.dart';

List connectList = [
  {
    "nickname": "default",
    "avatar": "default",
    "text": "default",
    "picture": [
      "images/connectPage/user1/1.jpg",
      "images/connectPage/user1/2.jpg",
      "images/connectPage/user1/3.jpg",
    ],
    "favor": 520,
    "comment": []
  },
  {
    "nickname": "default",
    "avatar": "default",
    "text": "default",
    "picture": [
      "images/connectPage/user2/1.jpg",
      "images/connectPage/user2/2.jpg",
      "images/connectPage/user2/3.jpg",
    ],
    "favor": 215,
    "comment": []
  },
];

class quickView extends StatefulWidget {
  final nickname, avatar, text, picture, favor, comment;
  quickView(this.nickname, this.avatar, this.text, this.picture, this.favor,
      this.comment);
  @override
  _quickViewState createState() => _quickViewState(this.nickname, this.avatar,
      this.text, this.picture, this.favor, this.comment);
}

class _quickViewState extends State<quickView> {
  int favor;
  final nickname, avatar, text, picture, comment;

  _quickViewState(this.nickname, this.avatar, this.text, this.picture,
      this.favor, this.comment);
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Divider(thickness: 10, color: defaultgreen),
        Card(
            child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            UserSynopsis(),
            Padding(
              padding: EdgeInsets.fromLTRB(15, 0, 15, 15),
              child: Row(
                children: <Widget>[
                  Text(text == "default" ? "无文案" : text,
                      style: TextStyle(color: darkgreen)),
                ],
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: picture == null
                  ? null
                  : <Widget>[
                      Container(
                        height: 110,
                        width: 110,
                        color: defaultgreen,
                        child: FlatButton(
                          onPressed: () {
                            Navigator.of(context).push(FadeRoute(
                              page: PhotoViewSimpleScreen(
                                imageProvider: AssetImage(picture[0]),
                              ),
                            ));
                          },
                          child: Image.asset(picture[0], fit: BoxFit.fill),
                          padding: EdgeInsets.all(0),
                          color: Colors.white,
                        ),
                      ),
                      Container(
                        height: 110,
                        width: 110,
                        color: defaultgreen,
                        child: FlatButton(
                          onPressed: () {
                            Navigator.of(context).push(FadeRoute(
                              page: PhotoViewSimpleScreen(
                                imageProvider: AssetImage(picture[1]),
                              ),
                            ));
                          },
                          child: Image.asset(picture[1], fit: BoxFit.fill),
                          padding: EdgeInsets.all(0),
                          color: Colors.white,
                        ),
                      ),
                      Container(
                        height: 110,
                        width: 110,
                        color: defaultgreen,
                        child: FlatButton(
                          onPressed: () {
                            Navigator.of(context).push(FadeRoute(
                              page: PhotoViewSimpleScreen(
                                imageProvider: AssetImage(picture[2]),
                              ),
                            ));
                          },
                          child: Image.asset(picture[2], fit: BoxFit.fill),
                          padding: EdgeInsets.all(0),
                          color: Colors.white,
                        ),
                      ),
                    ],
            ),
            SizedBox(height: 25),
          ],
        )),
        SizedBox(height: 5),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            LikeButton(
              likeCount: favor,
              countDecoration: (Widget count) {
                return Row(
                  children: <Widget>[
                    SizedBox(width: 5),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        count,
                        Text(
                          "收藏",
                          style: TextStyle(color: darkgreen),
                        ),
                      ],
                    ),
                  ],
                );
              },
            ),
            LikeButton(
              likeCount: comment.length,
              likeBuilder: (bool isComment) {
                return Icon(
                  Icons.comment,
                  color: isComment ? Colors.blue : defaultgreen,
                );
              },
              countDecoration: (Widget count) {
                return Row(
                  children: <Widget>[
                    SizedBox(width: 5),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        count,
                        Text(
                          "评论",
                          style: TextStyle(color: darkgreen),
                        ),
                      ],
                    ),
                  ],
                );
              },
            ),
            /*
            RaisedButton(
                onPressed: () {
                  setState(() {
                    this.favor++;
                  });
                },
                color: Colors.white,
                child: Row(
                  children: <Widget>[
                    Icon(Icons.favorite, color: Colors.red),
                    SizedBox(width: 15),
                    Column(
                      children: <Widget>[
                        Text(
                          "收藏",
                          style: TextStyle(color: defaultgreen),
                        ),
                        Text("$favor"),
                      ],
                    )
                  ],
                )),
            RaisedButton(
                onPressed: () {},
                color: Colors.white,
                child: Row(
                  children: <Widget>[
                    Icon(Icons.chat_bubble, color: Colors.red),
                    SizedBox(width: 15),
                    Column(
                      children: <Widget>[
                        Text(
                          "评论",
                          style: TextStyle(color: defaultgreen),
                        ),
                        Text("${comment.length}"),
                      ],
                    )
                  ],
                )),
          */
          ],
        ),
        SizedBox(height: 5),
      ],
    );
  }
}

class ConnectPage extends StatefulWidget {
  ConnectPage({Key key}) : super(key: key);
  @override
  _ConnectPageState createState() => _ConnectPageState();
}

class _ConnectPageState extends State<ConnectPage> {
  final _scrollController = ScrollController();
  final ViewList = List.generate(5, (i) => i);
  bool isLoading = false;

  void initState() {
    super.initState();
    _scrollController.addListener(() {
      if (_scrollController.offset ==
          _scrollController.position.maxScrollExtent) {
        loadMore(ViewList.length);
      }
    });
  }

  void loadMore(int from) async {
    if (!isLoading) {
      setState(() {
        isLoading = true;
      });
    }
    await Future.delayed(Duration(seconds: 2), () {
      setState(() {
        isLoading = false;
        ViewList.addAll(List.generate(5, (i) => i + from));
      });
    });
  }

  Widget getViewList(context, index) {
    if (index == 0)
      return Padding(
        padding: EdgeInsets.fromLTRB(15, 10, 15, 10),
        child: FlatButton(
            onPressed: () {},
            child: Text("点击搜索关键词"),
            textColor: darkgreen,
            color: defaultyellow),
      );
    if (index == ViewList.length) {
      return Center(
          child: Opacity(
        opacity: isLoading ? 1.0 : 0.0,
        child: CircularProgressIndicator(),
      ));
    }
    return quickView(
        connectList[(index - 1) % connectList.length]["nickname"],
        connectList[(index - 1) % connectList.length]["avatar"],
        connectList[(index - 1) % connectList.length]["text"],
        connectList[(index - 1) % connectList.length]["picture"],
        connectList[(index - 1) % connectList.length]["favor"],
        connectList[(index - 1) % connectList.length]["comment"]);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: Icon(Icons.menu),
        centerTitle: true,
        backgroundColor: Colors.white,
        elevation: 5,
        title: Text(
          '平台',
          style: TextStyle(color: darkgreen),
        ),
      ),
      body: ListView.builder(
        controller: _scrollController,
        itemCount: ViewList.length + 1,
        itemBuilder: getViewList,
      ),
    );
  }
}
